<template>
  <div class="xtx-goods-page">
    <div class="container m-top-20" v-if="goodData && goodData.brand">
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <!-- 图片预览区 -->
          <XtxImageView :image-list="goodData.mainPictures" />
          <!-- 统计数量 -->
          <GoodSale :good-data="goodData" />
        </div>
        <div class="spec">
          <!-- 商品信息区 -->
          <GoodInfo :good-data="goodData" />
          <!-- sku组件 -->
          <XtxGoodSku :goods="goodData" @change="skuChange" />
          <!-- 数据组件 -->

          <CheckNumber :min="2" :max="5" v-model="num" />
          <!-- 按钮组件 -->
          <XtxButton type="primary" style="margin-top: 20px" @click="insertCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品详情 -->
          <GoodDetail :good-data="goodData" />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodHot :type="1" />
          <GoodHot :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodSale from './components/good-sale.vue'
import GoodInfo from './components/good-info.vue'
import GoodDetail from './components/good-detail.vue'
import GoodHot from './components/good-hot.vue'
import { findGoods } from '@/api/good'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import router from '@/router'
import Message from '@/components/Message/index.js'
export default {
  components: {
    GoodSale,
    GoodInfo,
    GoodDetail,
    GoodHot
  },
  setup () {
    // 这个地方组件会渲染两次  一次是使用空对象 一次是数据从后端返回之后使用完整数据再渲染一次
    // 首次使用空对象渲染的时候
    // 解决办法: v-if手动控制渲染时机 只有表达式的值为true才正式开始首次渲染
    const goodData = ref({})
    const route = useRoute()
    async function loadData () {
      const res = await findGoods(route.params.id)
      goodData.value = res.data.result
    }
    loadData()

    // sku
    let skuObj = {}
    function skuChange (sku) {
      console.log(sku)
      skuObj = sku
    }

    const num = ref(3)

    // 加入购物车
    const store = useStore()
    async function insertCart () {
      // 是否登录
      const token = store.state.user.profile.token
      if (!token) {
        router.push({
          path: '/login',
          query: {
            redirect: encodeURIComponent(route.fullPath)
          }
        })
      } else {
        // 已经登录
        await store.dispatch('cart/asyncInsertCart', { skuId: skuObj.skuId, count: num.value })
        // 提示用户
        Message({ text: '加入购物成功', type: 'success' })
      }
    }
    return {
      goodData,
      skuChange,
      num,
      insertCart
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

.number-box {
  display: flex;
  align-items: center;
  .label {
    width: 60px;
    color: #999;
    padding-left: 10px;
  }
}
</style>
